package com.iman.common.util;

import java.lang.Thread.State;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UtilEx {
	

	public static boolean isIncludeString(String str, String match)
	{
		Pattern p=Pattern.compile(match);
		Matcher m=p.matcher(str);

		if(m.find())
		{
			String strmobile = "手机号码";
			//System.out.println(strmobile);
			
			return true;
		}
		return false;
	}
	public static boolean isDigit(String str)
	{
		if (str == null || str.equals(""))
    		return false;
    			
    			
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(str);
        return m.matches();
	}
    public static  boolean isMobileNO(String mobiles) {
    	if (mobiles == null || mobiles.equals(""))
    		return false;
    			
    			
        Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(14[0-9])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }
	
	public static List<String> splitString(String str, String delimiter)
	{
		String [] result = str.split(delimiter);
	
		if(result != null && result.length > 0)
			return Arrays.asList(result);
		
		return null;
	}
	

	

	public static List<Long> toLongList(List<String> strList) {
		if(strList == null)
			return null;
		List<Long> longList = new ArrayList<Long>();
		
		for(int i = 0; i < strList.size(); i++)
		{
			longList.add(Long.parseLong(strList.get(i)));
		}
		
		return longList;
	}
	
	
	
	public static String getPropValue(String fileName, String key)
	{
		try {
			ResourceBundle rb = ResourceBundle.getBundle(fileName);	
			return rb.getString(key);   
		} catch (Exception e)
		{
			return null;
		}
	}
	
	
	public static ResourceBundle getPropValue(String fileName )
	{
		try {
			ResourceBundle rb = ResourceBundle.getBundle(fileName);	
			return rb ;   
		} catch (Exception e)
		{
			return null;
		}
	}
	
	
	
	
	public static boolean threadFree(Thread thread)
	{
		State state = thread.getState();
		if(state != State.NEW && state != State.TERMINATED && state != State.WAITING ) {
			return false;
		}
		return true;
	}
	/*
	public static void checkThread(Thread thread)
	{
		State state = thread.getState();
	
		if(state == State.NEW || state == State.TERMINATED)
			thread.start();
	}
	*/
	
	
	public static String getSimpleDate(Date date)
	{
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		
		if(date == null) {
			Long cur = Calendar.getInstance().getTimeInMillis();
			return timeFormat.format(new Date(cur));
		} else 
			return timeFormat.format(date);
	}
	
	public static String getReportTime(Date date)
	{
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss"); 
		
		if(date == null) {
			Long cur = Calendar.getInstance().getTimeInMillis();
			return timeFormat.format(new Date(cur));
		} else 
			return timeFormat.format(date);
	}
	
	
	public static String getCurTime()
	{
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Long cur = Calendar.getInstance().getTimeInMillis();
		return timeFormat.format(new Date(cur));
	}
	
	
	
	public static int getCurMonth()
	{
		SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMM"); 
		Long cur = Calendar.getInstance().getTimeInMillis();
		Date curDate = new Date(cur);
		String dateTmp = timeFormat.format(new Date(cur));
		
		return Integer.parseInt(dateTmp);
	}
	
	public static Double doubleFormat(Double data, int fraction)
	{
		NumberFormat ddf1=NumberFormat.getNumberInstance();
		ddf1.setMaximumFractionDigits(fraction); 
		String s= ddf1.format(data); 
		
		Number num = null;
		
		try {
			num = ddf1.parse(s);
			
			if(num != null) {
				Double d = num.doubleValue();
				return d;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return Double.parseDouble(s);
	}
}
